package com.heima.wemedia.controller;

import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.media.dtos.WmMaterialDto;
import com.heima.wemedia.entity.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class MaterialController {

    @Autowired
    private WmMaterialService materialService;

    @PostMapping("/api/v1/material/upload_picture")
    public ResponseResult<String> uploadPic(MultipartFile multipartFile){
        return ResponseResult.ok(materialService.uploadPic(multipartFile));
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/material/list")
    public PageResult<WmMaterial> findByPage(@RequestBody WmMaterialDto dto){
        return materialService.findByPage(dto);
    }


    /**
     * 收藏
     * @param id
     * @return
     */
    @PutMapping("/api/v1/material/collect/{id}")
    public ResponseResult collect(@PathVariable(name = "id")Integer id){
        materialService.updateCollect(id,1);
        return ResponseResult.ok();
    }

    /**
     * 取消收藏
     * @param id
     * @return
     */
    @PutMapping("/api/v1/material/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable(name = "id")Integer id){
        materialService.updateCollect(id,0);
        return ResponseResult.ok();
    }


    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/api/v1/material/del_picture/{id}")
    public ResponseResult delete(@PathVariable(name = "id")Integer id){
        materialService.deleteWmMaterial(id);
        return ResponseResult.ok();
    }


}
